{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "abc6b774",
   "metadata": {},
   "source": [
    "<font color=red>1. 给定一个字符串，将字符串前面的若干字符移动到字符串的尾部。比如。将`abcdef`的前三个字符移到末尾，原来字符串将变成`defabc` </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "050b9b59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len = 7\n",
      "pre : abcdef\n",
      "after : defabc\n"
     ]
    }
   ],
   "source": [
    "//逐个移动\n",
    "#include <iostream>\n",
    "void leftString(char *s, int n, int m) {\n",
    "    auto leftShiftOne = [](char *s,int n) {\n",
    "        char t = s[0];\n",
    "        for(int i = 1; i < n; i++){\n",
    "            s[i - 1] = s[i];\n",
    "        }\n",
    "        s[n - 1] = t;\n",
    "    };\n",
    "    while(m--){\n",
    "        leftShiftOne(s, n);\n",
    "    }\n",
    "}\n",
    "char str[]=\"abcdef\";\n",
    "int len = sizeof(str)/sizeof(*str);\n",
    "std::cout <<\"len = \"<<len<< std::endl;\n",
    "std::cout <<\"pre : \"<< str << std::endl;\n",
    "leftString(str, len - 1, 3);\n",
    "std::cout <<\"after : \"<< str << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6782fd88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len = 7\n",
      "pre : abcdef\n",
      "after : defabc\n"
     ]
    }
   ],
   "source": [
    "//三步反转\n",
    "void leftString(char *s, int n, int m){\n",
    "    auto reverseString = [](char *s, int begin, int end){\n",
    "        while(begin < end){\n",
    "            char t = s[begin];\n",
    "            s[begin++] = s[end];\n",
    "            s[end--] = t;\n",
    "        }\n",
    "    };\n",
    "    m %= n;\n",
    "    reverseString(s, 0, m - 1);\n",
    "    reverseString(s, m, n - 1);\n",
    "    reverseString(s, 0, n - 1);\n",
    "}\n",
    "char str[]=\"abcdef\";\n",
    "int len = sizeof(str)/sizeof(*str);\n",
    "std::cout <<\"len = \"<<len<< std::endl;\n",
    "std::cout <<\"pre : \"<< str << std::endl;\n",
    "leftString(str, len - 1, 3);\n",
    "std::cout <<\"after : \"<< str << std::endl;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++11",
   "language": "C++11",
   "name": "xcpp11"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
